#! c:\\Perl\\bin\\perl.exe w

use strict;
use ADN::LIBRARY::ADAPTERS::Execute;
use ADN::XML::OBJtoXML;
use ADN::LIBRARY::SOAP::ClientSOAP;
use MIME::Base64;

my $ip = $ARGV[0];
my $Srep = $ARGV[1];
my $param = $ARGV[2];

main();
exit 0;

sub main {

  my ( $Log, $nbr, $service, $xml, $ip_equip, $model,
        $type, $filtre, $ident, $result_log, $protocol );
  my ( @tab, @prm, @result );

  @tab = split /\%\!\%\*\%\~\%/smx, $param;
  $nbr = scalar @tab;
  print "NBR : $nbr\n";

  for ( my $i = 0; $i < $nbr; $i++ ) {
    @prm = split /\@\#\@\*\@\~\@/smx, $tab[$i];
  }

  $nbr = scalar @prm;
  print "NBR PRM : $nbr\n";

  if ($nbr !~ /^[78]$/smx) {
    print 'Error ARG !!!';
    exit 0;
  }

  print "Arg 1 : $prm[0]";
  print "\nArg 2 : $prm[1]";
  print "\nArg 3 : $prm[2]";
  print "\nArg 4 : $prm[3]";
  print "\nArg 5 : $prm[4]";
  print "\nArg 6 : $prm[5]";
  print "\nArg 7 : $prm[6]\n";
  print "Arg 8 encode : $prm[7]\n" if (defined $prm[7]);
  $prm[7] = decode_base64($prm[7]) if (defined $prm[7]);
  print "Arg 8 decode : $prm[7]\n\n" if (defined $prm[7]);

  $ip_equip = $prm[0];
  $service = $prm[1];
  $model = $prm[2];
  $type = $prm[3];
  $filtre = $prm[4];
  $ident = $prm[5];
  $protocol = $prm[6];
  
  $ident =~ s/^\{|\}$//gsmx;
  $ident =~ s/\},\{/#ADN#/gsmx;
  $ident =~ s/,/:ADN:/gsmx;

  $Log = ADN::LIBRARY::ADAPTERS::Execute->new();
  $result_log = $Log->$model($type, $service, $ip_equip, $ident, $protocol, $prm[7]) if (defined($prm[7]));
  $result_log = $Log->$model($type, $service, $ip_equip, $ident, $protocol, undef) if (!defined($prm[7]));

  print "\nDATA decode : $result_log";

  $result_log = encode_base64($result_log) if ($service ne 'restore');

  $xml = ADN::XML::OBJtoXML->new();
  $xml->Data($result_log);
  $xml->Ip($ip_equip);
  $xml->$service($xml);
  @result = $xml->Filtre($filtre, 'sauv');

  print "\n\nDATA : ";
  print @result;

  ADN::LIBRARY::SOAP::ClientSOAP->new( Proxy => $ip,
						                           Service => $Srep,
						                           Data => \@result );
  return;
}
